﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows.Data;
using System.Windows;
using System.Windows.Input;
using System.Windows.Shapes;
using System.Windows.Controls;
using Shoozla.View;
using Shoozla.DataModel;
using System.IO;
using System.Threading;


namespace Shoozla.ViewModel
{
    /// <summary>
    /// The ViewModel for the application's main window.
    /// </summary>
    public class MainWindowViewModel : ViewModelBase
    {

        #region Commands       

        
        private RelayCommand _CmdStopThreads;
        public RelayCommand CmdStopThreads
        {
            get
            {
                if (_CmdStopThreads == null)
                {
                    _CmdStopThreads = new RelayCommand(param => DoCmdStopThreads(), param => { return !CanExecuteUI(); });
                }
                return _CmdStopThreads;
            }
        }

        private RelayCommand _CmdAbout;
        public RelayCommand CmdAbout
        {
            get
            {
                if (_CmdAbout == null)
                {
                    _CmdAbout = new RelayCommand(param => DoCmdAbout(), param => { return true; });
                }
                return _CmdAbout;
            }
        }

        private RelayCommand _CmdOpenLog;
        public RelayCommand CmdOpenFLog
        {
            get
            {
                if (_CmdOpenLog == null)
                {
                    _CmdOpenLog = new RelayCommand(param => DoCmdOpenFLog(), param => { return true; });
                }
                return _CmdOpenLog;
            }
        }

        private RelayCommand _cmdOpenList;
        public ICommand CmdOpenList {
            get {
                if (_cmdOpenList == null)
                {
                    _cmdOpenList = new RelayCommand(param => DoCmdOpenList(), param => CanExecuteUI());
                }
                return _cmdOpenList;
            }
        }

        private RelayCommand _cmdOpenPreview;
        public ICommand CmdOpenPreview
        {
            get
            {
                if (_cmdOpenPreview == null)
                {
                    _cmdOpenPreview = new RelayCommand(param => DoCmdOpenPreview(), param => CanExecuteUI());
                }
                return _cmdOpenPreview;
            }
        }

        private DirectoryInfo _currentFolder;
        public DirectoryInfo CurrentFolder
        {
            get { return _currentFolder; }
            set {
                if (value != _currentFolder)
                {
                    _currentFolder = value;
                    OnPropertyChanged("CurrentFolder");
                    OnPropertyChanged("FilesRead");
                    OnPropertyChanged("Mp3Read");
                }
            }
        }

        public int FilesRead
        {
            get
            {
                if (CurrentFolder == null) return 0;
                else
                {
                    FileInfo[] files = CurrentFolder.GetFiles("*.*", SearchOption.AllDirectories);
                    return files.Count();
                }
            }
        }

        public int Mp3Read
        {
            get
            {
                if (CurrentFolder == null) return 0;
                else
                {
                    try
                    {
                        FileInfo[] files = CurrentFolder.GetFiles("*.mp3", SearchOption.AllDirectories);
                        return files.Count();
                    }
                    catch (UnauthorizedAccessException accEx)
                    {
                        //TaskDialogClass.TaskDialog(
                        //  new System.Windows.Interop.WindowInteropHelper((Window)App.MainView).Handle,
                        //  IntPtr.Zero, "Shoozla", "Access Denied", "You cannot access to the folder or one of its subfolders: " + CurrentFolder,
                        //  TaskDialogButtons.OK,
                        //  TaskDialogIcon.Warning
                        //   );
                        MessageBox.Show("Access Denied.\n You cannot access to the folder or one of its subfolders: " + CurrentFolder, "Shoozla", MessageBoxButton.OK);
                        return 0;
                    }
                }
            }
        }

        #endregion

        #region Constructor

        public MainWindowViewModel()
        {
          //  base.DisplayName = Strings.MainWindowViewModel_DisplayName;
            base.DisplayName = "Shoozla Main Window";
            DoCmdOpenList();
        }

        #endregion // Constructor

        #region Do Commands

        private void DoCmdStopThreads()
        {
            App.StopWorkingThreads();
        }

        private void DoCmdOpenList()
        {
            CurrentView = App.FolderView;
        }

        private void DoCmdOpenPreview()
        {
            CurrentView = App.PreviewView;
        }

        private void DoCmdOpenFLog()
        {
            App.Tracer.Save();
            System.Diagnostics.Process.Start("notepad.exe", App.LogFileName);
        }

        private void DoCmdAbout()
        {
            AboutView aboutW = new AboutView();
            aboutW.ShowDialog();

        }

        #endregion

        public void ShowNextWindow()
        {
            switch (App.CurrenAppStatus)
            {
                case App.AppStatus.ReadingFiles: { break; }
                case App.AppStatus.SearchingCovers: { CurrentView = App.PreviewView; break; }
                case App.AppStatus.ApplyingCovers: { 
                    //refresh of the current opened folder
                    
                   // App.ProgressBarWindow.StopProgressBar();
                    //new Thread(new ThreadStart(()=>{
                   // Thread.Sleep(1000);
                     //TaskDialogClass.TaskDialog(
                     //  new System.Windows.Interop.WindowInteropHelper((Window)App.MainView).Handle,
                     //  IntPtr.Zero, "Shoozla", "Covers saved successfully", "Total number of saved files: " + App.Logger.FilesSaved,
                     //  TaskDialogButtons.OK,
                     //  TaskDialogIcon.SecuritySuccess
                     //   );
                    MessageBox.Show((Window)App.MainView,"Total number of saved covers: " + App.Logger.FilesSaved, "Shoozla", MessageBoxButton.OK, MessageBoxImage.Information);
                    //})).Start();
                    CurrentView = App.FolderView;
                  //  ((FolderListViewModel)App.FolderView.DataContext).RefreshOpenedFolder();
                    break; }
                default: { break; }
            }
        }
    }
}